/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.io;

import java.io.PrintWriter;
import java.io.StringWriter;
import static org.opu.vcforms.resource.ResourceHandle.*;

/**
 *
 * @author yura
 */
public class Logger {

    public static final String JNU_ENCODING = System.getProperty("sun.jnu.encoding");
    public static final String FILE_ENCODING = System.getProperty("file.encoding", JNU_ENCODING);
    public static final String CONSOLE_ENCODING = System.getProperty("console.encoding", FILE_ENCODING);
    public static final String SEPARATOR = System.getProperty("line.separator", "\n");

    static {
        if (isDebugAvailable()){
            System.out.println("Console encoding: " + CONSOLE_ENCODING);
            System.out.println("Line separator: " + SEPARATOR.replaceAll("\r", "\\r").replaceAll("\n", "\\\\n"));
        }
    }

    public static void debug(String msg){
        if (isDebugAvailable()){
            sout("[  DEBUG]: " + msg);
        }
    }

    public static void infor(String msg){
        if (isInfoAvailable()){
            sout("[   INFO]: " + msg);
        }
    }

    public static void warning(String msg){
        if (isWarningAvailable()){
            sout("[WARNING]: " + msg);
        }
    }

    public static void error(String msg){
        if (isErrorAvailable()){
            serr("[  ERROR]: " + msg);
        }
    }

    public static void error(Exception ex){
        if (isErrorAvailable()){
            serr("[  ERROR]: " + ex);
            serrEx("[  ERROR]: ", ex);
        }
    }

    public static void fatal(String msg){
        if (isFatalAvailable()){
            serr("[  FATAL]: " + msg);
        }
    }

    public static void fatal(Exception ex){
        if (isFatalAvailable()){
            serr("[  FATAL]: " + ex);
            serrEx("[  FATAL]: ", ex);
        }
    }

    public static void sout(String msg) {
        try {
            byte[] bytes = (msg + SEPARATOR).getBytes(CONSOLE_ENCODING);
            System.out.write(bytes);
            System.out.flush();
        } catch (Exception ex) {
            System.out.println(msg);
        }
    }

    public static void serr(String msg) {
        try {
            byte[] bytes = (msg + SEPARATOR).getBytes(CONSOLE_ENCODING);
            System.err.write(bytes);
            System.err.flush();
        } catch (Exception ex) {
            System.err.println(msg);
        }
    }

    public static void serrEx(String prefix, Exception ex) {
        StringWriter sw = new StringWriter();
        ex.printStackTrace(new PrintWriter(sw));
        serr(prefix + sw.toString().replaceAll("\n", "\n" + prefix));
    }
    
}
